﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using WindowsPhoneToolbox.Util;
using AppRes = WindowsPhoneToolbox.Resources;

namespace WindowsPhoneToolbox.Controls
{
    /// <summary>
    /// Interaction logic for AppEmulatorControl.xaml
    /// </summary>
    public partial class AppEmulatorControl
    {
        private static bool IsInDesignModeStatic
        {
            get
            {
                return
                    (bool)
                    DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty, typeof(FrameworkElement)).Metadata.DefaultValue;
            }
        }

        public AppEmulatorControl()
        {
            InitializeComponent();
            DataContext = this;
            if (!IsInDesignModeStatic)
            {
                LoadAppInst(null);
            }
            else
            {
                NoteList.Text = AppRes.NoteAppInst;
                LoadAppInstDesign();
            }
        }

        public static readonly RoutedEvent ActionEvent =
            EventManager.RegisterRoutedEvent("ActionCommand", RoutingStrategy.Bubble,
            typeof(RoutedEventHandler), typeof(AppEmulatorControl));

        public event RoutedEventHandler ActionCommand
        {
            add { AddHandler(ActionEvent, value); }
            remove { RemoveHandler(ActionEvent, value); }
        }

        public static readonly DependencyProperty TitleExpandProperty =
        DependencyProperty.Register("TitleExpand", typeof(string), typeof(AppEmulatorControl), new PropertyMetadata(default(string), TitleExpandPropertyChangedCallback));

        private static void TitleExpandPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            ((AppEmulatorControl)dependencyObject).TitleExp.Text = (string)dependencyPropertyChangedEventArgs.NewValue;
        }

        public string TitleExpand
        {
            get { return (string)GetValue(TitleExpandProperty); }
            set
            {
                SetValue(TitleExpandProperty, value);
            }
        }

        public static readonly DependencyProperty ExpandedProperty =
        DependencyProperty.Register("Expanded", typeof(bool), typeof(AppEmulatorControl), new PropertyMetadata(default(bool), ExpandedPropertyChangedCallback));

        private static void ExpandedPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var value = (bool)dependencyPropertyChangedEventArgs.NewValue;
            ((AppEmulatorControl)dependencyObject).expAppIns.IsExpanded = value;
        }

        public bool Expanded
        {
            get { return (bool)GetValue(ExpandedProperty); }
            set
            {
                SetValue(ExpandedProperty, value);
                expAppIns.IsExpanded = value;
            }
        }


        public static readonly DependencyProperty NoteProperty =
        DependencyProperty.Register("Note", typeof(string), typeof(AppEmulatorControl), new PropertyMetadata(default(string), NotePropertyChangedCallback));

        private static void NotePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var value = (string) dependencyPropertyChangedEventArgs.NewValue;
            ((AppEmulatorControl)dependencyObject).NoteList.Visibility = string.IsNullOrEmpty(value) ? Visibility.Collapsed : Visibility.Visible;
            ((AppEmulatorControl)dependencyObject).NoteList.Text = value;
        }

        public string Note
        {
            get { return (string)GetValue(NoteProperty); }
            set
            {
                SetValue(NoteProperty, value);
                NoteList.Visibility = string.IsNullOrEmpty(value) ? Visibility.Collapsed : Visibility.Visible;
            }
        }

        public static readonly DependencyProperty RemoteApplicationsProperty =
            DependencyProperty.Register("RemoteApplications", typeof(Collection<AppPhoneRemoteApplication>), typeof(AppEmulatorControl), new PropertyMetadata(default(Collection<AppPhoneRemoteApplication>), RemoteApplicationsPropertyChangedCallback));

        private static void RemoteApplicationsPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            ((AppEmulatorControl)dependencyObject).RemoteApplications = (Collection<AppPhoneRemoteApplication>)dependencyPropertyChangedEventArgs.NewValue;
        }

        public Collection<AppPhoneRemoteApplication> RemoteApplications
        {
            get { return (Collection<AppPhoneRemoteApplication>)GetValue(RemoteApplicationsProperty); }
            set
            {
                SetValue(RemoteApplicationsProperty, value);
                LoadAppInst(value);
            }
        }

        private void LoadAppInstDesign()
        {
            expAppIns.IsExpanded = true;
            var appinst = new List<AppPhoneRemoteApplication>
                              {
                                  new AppPhoneRemoteApplication
                                      {
                                          ProductId = Guid.NewGuid().ToString(),
                                          ProductName = "Sample App1"
                                      },
                                  new AppPhoneRemoteApplication
                                      {
                                          ProductId = Guid.NewGuid().ToString(),
                                          ProductName = "Sample App2"
                                      }
                              };
            LstAppInst.ItemsSource = appinst;
        }

        private void LoadAppInst(IEnumerable<AppPhoneRemoteApplication> data)
        {
            LstAppInst.ItemsSource = data;
        }

        private void AppPhoneControlActionCommand(object sender, RoutedEventArgs e)
        {
            var tempact = ActionEvent;
            if (tempact == null) return;

            var temp = (AppPhoneEvent)e;
            var arg = new AppPhoneEvent(temp.Action, temp.Name, temp.Guid, temp.Alias) { RoutedEvent = tempact };

            RaiseEvent(arg);
        }
    }
}
